package com.itheima.utils;

import com.itheima.domin.pojo.Emp;
import com.itheima.domin.pojo.Log;
import com.itheima.domin.vo.Result;
import com.itheima.exception.ClientException;
import com.itheima.filter.CurrentHolder;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.ReportService;
import io.jsonwebtoken.Claims;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;

/**端口调动监测
 * @Author:上官徵羽
 * @Date: 2025/07/16 15:59
 * @Description TODO 今日内容:
 */
@Component
/** 当前类为切面类 */
@Aspect
@Slf4j
public class LogAndAOP {
    @Autowired
    private ReportService reportService;
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    /** 定义切点:controller包下的所有方法 */
    @Pointcut("execution(* com.itheima.controller..*.*(..))")
    public void requestLog(){}

    /** 环绕记录各种信息参数 */
    @Around("requestLog()")
    public Object recordBegan(ProceedingJoinPoint proceedingJoinPoint) throws Exception{
        //开始记录时间
        long beganTime = System.currentTimeMillis();

        try {
            //执行原始方法
            Object result = proceedingJoinPoint.proceed();
            //记录结束时间
            long endTime = System.currentTimeMillis();
            saveLog(proceedingJoinPoint,result,endTime-beganTime);

            return result;
        } catch (Throwable e) {
            log.error("LogAndAOP recordBegan:{} LogAndAOP文件记录数据处理出现问题",proceedingJoinPoint);
            e.printStackTrace();
            throw new ClientException("出问题了");
        }
    }


    /** 构建日志对象并保存 */
    public void saveLog(ProceedingJoinPoint proceedingJoinPoint,Object o,long time){
        //处理数据
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();
        //获取令牌内容
        Claims claims = CurrentHolder.getCurrentHolder();
        Integer id = claims.get("id", Integer.class);
        Emp emp = empService.selectIdEmp(id);
        log.info("员工信息:{}",emp.toString());


        Log log1 = new Log();
        log1.setOperateTime(LocalDateTime.now());
        log1.setClassName(proceedingJoinPoint.getTarget().getClass().getName());
        log1.setMethodName(method.getName());
        log1.setReturnValue(Result.success(o).getCode().toString()+"\t"+ Result.success(o).getMsg());
        log1.setCostTime((int) time);
        log1.setMethodParams(deptService.selectId(emp.getDeptId()).toString());
        log1.setOperateEmpId(id);
        log1.setOperateEmpName(emp.getName());
        reportService.addLog(log1);
    }

}
